﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using WebBanThuoc.Server;
using WebBanThuoc.DAO;
using WebBanThuoc.DTO;

namespace WebBanThuoc
{
    public partial class Login : System.Web.UI.Page
    {
        IServerWeb sw = new ServerWeb();

        protected void Page_Load(object sender, EventArgs e)
        {
           //if(Request.Cookies["StudentCookies"] != null)
           //   tb_email.Text = Request.Cookies["email_login"].Value; 
            this.LoadComplete += new EventHandler(Login_LoadComplete);
        }
        void Login_LoadComplete(object sender, EventArgs e)
        {
            sw.closeConnection();
        }
        
        

      protected void bt_dangnhap_Click(object sender, EventArgs e)
        {
            if (tb_email.Text != "")
            {

                if (!Function.Func.CheckFormatEmailAdress(tb_email.Text))
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "errorLogin", "<script language='javascript'>alert('Email không đúng định dạng');</script>", false);
                    return;
                }
                KhachHang_DAO daoKH = new KhachHang_DAO(sw.Connection);
                string email_login = tb_email.Text.Trim();
                string mk = tb_matkhau.Text.Trim();
                KhachHang kh = daoKH.loginByParam(email_login, mk);
                if (kh != null)
                {
                    LinkButton l = (LinkButton)Master.FindControl("linkLog1");
                    if (l != null)
                        l.Text = "Đăng xuất";

                    /*if (remember.Checked)
                    {
                        HttpCookie cookie_email = new HttpCookie("email_login");
                        cookie_email.Value = email_login;
                        cookie_email.Expires = DateTime.Now.AddDays(10);
                        Response.Cookies.Add(cookie_email);
                    } */
                    Session.Add("kh", kh);
                    if (Session["trangTruoc"] != null)
                    {
                        string trangTruoc = Session["trangTruoc"].ToString();
                        Response.Redirect(trangTruoc);
                    }
                    else
                        Response.Redirect("TrangChu.aspx");
                }
                else
                    label1.Text = "Thông tin đăng nhập chưa đúng.";

            }
            else
                label1.Text = "Vui lòng nhập email.";

        }

      protected void remember_CheckedChanged(object sender, EventArgs e)
      {

      } 

        
       
    }
}